import {BaseInfo} from "db://assets/scripts/data/model/BaseInfo";
import {BaseFieldEnum} from "db://assets/scripts/data/db/BaseFieldEnum";
import {CSV} from "db://assets/scripts/common/CSV";
import {Vec3Converter} from "db://assets/scripts/data/converter/Vec3Converter";
import {GameObjectType} from "db://assets/scripts/game/GameObjectType";

/**
 * 基础信息数据转换器
 * public convertFromVector: (V) -> T
 * public convertToVector: (T) -> V
 */
export class BaseInfoConverter {
    /**
     * 禁止创建对象
     */
    private constructor() {
    }

    /**
     * 转数组
     *
     * @param csv CSV数据
     */
    public static convertFromCSV(csv: CSV): BaseInfo[] {
        let result: BaseInfo[] = [];
        csv.datas.forEach(row => {
            result.push(this.convertFromRow(row));
        });
        return result;
    }

    /**
     * 从CSV行数据转到BaseInfo
     *
     * @param row CSV行数据
     */
    public static convertFromRow(row: string[]): BaseInfo {
        return {
            ID: row[BaseFieldEnum.ID],
            type: this.getGameObjectType(row[BaseFieldEnum.TYPE]),
            name: row[BaseFieldEnum.NAME],
            resName: row[BaseFieldEnum.RES_NAME],
            position: Vec3Converter.convertFromString(row[BaseFieldEnum.POSITION]),
            angle: Vec3Converter.convertFromString(row[BaseFieldEnum.ANGLE]),
            scale: Vec3Converter.convertFromString(row[BaseFieldEnum.SCALE]),
            hp: Number(row[BaseFieldEnum.HP]),
            attackPower: Number(row[BaseFieldEnum.ATTACK_POWER]),
            defensePower: Number(row[BaseFieldEnum.DEFENSE_POWER]),
            attackSpeed: Number(row[BaseFieldEnum.ATTACK_SPEED]),
            moveSpeed: Number(row[BaseFieldEnum.MOVE_SPEED]),
            moveFrequency: Number(row[BaseFieldEnum.MOVE_FREQUENCY]),
            dodgeRate: Number(row[BaseFieldEnum.DODGE_RATE]),
            criticalHitRate: Number(row[BaseFieldEnum.CRITICAL_HIT_RATE]),
            criticalHitDamage: Number(row[BaseFieldEnum.CRITICAL_HIT_DAMAGE]),
            moveMode: Number(row[BaseFieldEnum.MOVE_MODE]),
            movePattern: Number(row[BaseFieldEnum.MOVE_PATTERN]),
            goldNum: Number(row[BaseFieldEnum.GOLD_NUM]),
            heartDropRate: Number(row[BaseFieldEnum.HEART_DROP_RATE]),
        };
    }

    /**
     * 获取游戏对象类型
     *
     * @param key 键
     */
    private static getGameObjectType(key: string): GameObjectType {
        switch (key) {
            case GameObjectType.PLAYER:
                return GameObjectType.PLAYER;
            case GameObjectType.MONSTER:
                return GameObjectType.MONSTER;
            case GameObjectType.BOSS:
                return GameObjectType.BOSS;
            case GameObjectType.HEART:
                return GameObjectType.HEART;
            case GameObjectType.NPC:
                return GameObjectType.NPC;
            case GameObjectType.OBSTACLE:
                return GameObjectType.OBSTACLE;
        }
    }
}